package com.guigu.www.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guigu.www.pojo.*;
import com.guigu.www.service.CellService;
import com.guigu.www.service.GatherDetailsService;
import com.guigu.www.service.GatherService;
import com.guigu.www.service.imp.IdUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.RequestWrapper;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class GatherController {
    //仓库单元
    @Autowired
    CellService cellService;

    //获取service入库表
    @Autowired
    GatherService gatherService;
    //流水号工具类
    @Autowired
    IdUtils idUtils;
    //获取入库详情表service
    @Autowired
    GatherDetailsService gatherDetailsService;

    //入库 给入库表 入库明细表添加数据
    @RequestMapping("/addGatherGatherDetails.action")
    public Map addGatherGatherDetails(@RequestBody  Gather gather){
        //创建map
        Map map=new HashMap();
        //获取当前时间
        gather.setRegisterTime(new Date());
        //设置入库单编号
        gather.setGatherId(idUtils.getid6(gather));

        //S001-0: 等待审核
        //S001-1: 审核通过
        //S001-2: 审核不通过
        //设置状态
        gather.setCheckTag("S001-0");
        /*库存标志
        K002-1: 已登记
        K002-2: 已调度*/

        gather.setGatherTag("K002-1");
        //添加数据给入库表
        boolean save = gatherService.save(gather);
        //获取明细表
        List<GatherDetails> gatherDetails = gather.getGatherDetails();
        for (GatherDetails gatherDetail : gatherDetails) {
            gatherDetail.setGatherTag("K002-1");
            gatherDetail.setParentId(gather.getId());
        }
        System.out.println(gather);
        //调用方法
        boolean f=gatherDetailsService.addGathrDetailsd(gatherDetails);
        System.out.println(gather);
        //判断
        map.put("msg","入库申请失败");
        if(f && save){
           map.put("msg","入库申请成功 等待审核");
        }
        return map;
    }

    //入库表  入库申请审核
    @RequestMapping("/queryGatherById.action")
    public Gather queryGatherById(int id){
        //根据id查询要审核的入库产品
        Gather gather= gatherService.queryGatherById(id);
        return gather;
    }

    //查询所以要审核的库存产品审核
    @RequestMapping("/queryGatherAll.action")
    public PageVo<Gather> queryGatherAll(@RequestParam(value = "pageno",defaultValue ="1") int pageno,
                                         @RequestParam(value = "pagesize",defaultValue = "5") int pagesize){

        //获取方法
       IPage iPage=gatherService.queryGatherAll(pageno,pagesize);
       //创建pagevo
        PageVo<Gather> pageVo=new PageVo<Gather>();
        //设置值
        pageVo.setRows(iPage.getRecords());
        pageVo.setTotal(iPage.getTotal());

        return pageVo;
    }

    //审核 判断是否通过修改状态
    @RequestMapping("/updateGaherById.action")
    public Map updateGaherById(@RequestBody  Gather gather){
        //通过
        //审核标志
        //S001-0: 等待审核
        //S001-1: 审核通过
        //S001-2: 审核不通过
        if(gather.getPdid()==0){
            gather.setCheckTag("S001-1");
        }
        //不通过
        if(gather.getPdid()==1){
            gather.setCheckTag("S001-2");
        }
        System.out.println(gather);
        //获取方法
        boolean f=gatherService.updateGaherById(gather);
        //创建map
        Map map=new HashMap();
        //判断
        map.put("msg","审核失败");
        if(f){
            map.put("msg","审核成功");
        }

        return map;
    }

    //查询所以的入库申请查询
    @RequestMapping("/queryByGaherAllShow.action")
    public PageVo<Gather> queryByGaherAllShow(@RequestParam(value = "pageno",defaultValue ="1") int pageno,
                                            @RequestParam(value = "pagesize",defaultValue = "5") int pagesize){
        //获取方法
        IPage page=gatherService.queryByGaherAllShow(pageno,pagesize);
        //创建pagevo
        PageVo pageVo=new PageVo();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(page.getRecords());
       return pageVo;
    }


    //根据流水号查询入库的产品申请单
    @RequestMapping("/queryGaherBygatherId.action")
    public Gather queryGaherBygatherId(String gatherId){
        //获取方法
        return  gatherService.queryGaherBygatherId(gatherId);
    }


    //查询所以审核通过的入库申请单
    @RequestMapping("/queryBydispatgather.action")
    public PageVo<Gather> queryBydispatgather(@RequestParam(value = "pageno",defaultValue ="1") int pageno,
                                              @RequestParam(value = "pagesize",defaultValue = "5") int pagesize){
        //创建pagevo
        PageVo<Gather> pageVo=new PageVo<Gather>();
        //获取方法
        IPage<Gather> page=gatherService.queryBydispatgather(pageno,pagesize);
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }


    //根据入库单编号查询当前入库要调度的产品
    @RequestMapping("/queryBydispathchGatherdang.action")
    public Gather queryBydispathchGatherdang(String gatherId){
        return gatherService.queryGaherBygatherIds(gatherId);
    }



    //调度成功修改状态 为已调度
    @RequestMapping("/updateGatherBygatherId.action")
    public Map updateGatherBygatherId(String gatherId){
        //创建map
        Map map=new HashMap();
        //调用方法
        boolean f=gatherService.updateGatherBygatherId(gatherId);
        //判断
        map.put("msg","入库调度失败");
        if (f){
            map.put("msg","入库调度成功");
        }
        return  map;
    }



    //查询入库调度完成的 做登记
    @RequestMapping("/queryBywarehousinAll.action")
    public PageVo<Gather> queryBywarehousinAll(@RequestParam(value = "pageno",defaultValue ="1") int pageno,@RequestParam(value="pagesize",defaultValue ="5") int pagesize){
        //获取pagevo
        PageVo<Gather> pageVo=new PageVo<Gather>();

        //调用方法
       IPage<Gather> iPage=gatherService.queryBywarehousin(pageno,pagesize);
       pageVo.setTotal(iPage.getTotal());
       pageVo.setRows(iPage.getRecords());
        return  pageVo;
    }


    //根据登记的入库单号查询对应的入库数据 入库明细数据
    @RequestMapping("/queryByGatherGatherdetailswarhousing.action")
    public Gather queryByGatherGatherdetailswarhousing(String gatherId){
        //调用方法
       return gatherService.queryByGatherGatherdetailswarhousing(gatherId);
    }


    //登记提交  修改状态为登记
    @RequestMapping("/updateWarehosingGaher.action")
    public Map updateWarehosingGaher(@RequestBody Gather gather){
        System.out.println(gather);
        //创建map
        Map map=new HashMap();
        //修改入库表
       boolean f=gatherService.updateWarehosingGaher(gather);
       //修改明细表
        boolean f1=gatherDetailsService.updateWarehosingGaherDetils(gather.getGatherDetails(),gather.getId());
        //判断
        map.put("msg","登记失败");
        if(f && f1){
            map.put("msg","登记成功，等待审核");
        }
        return  map;
    }

    //查询所以的登记成功产品做审核
    @RequestMapping("/queryByWarehousingexaminepage.actiomn")
    public PageVo<Gather> queryByWarehousingexaminepage(@RequestParam(value = "pageno",defaultValue = "1") int pageno,
                                            @RequestParam(value = "pagesize",defaultValue ="5") int pagesize){

        //调用方法
       IPage<Gather> iPage=gatherService.queryByWarehousingexaminepage(pageno,pagesize);
        //创建pagevo
        PageVo<Gather> pageVo=new PageVo<Gather>();
        //赋值
        pageVo.setRows(iPage.getRecords());
        pageVo.setTotal(iPage.getTotal());
        return pageVo;
    }



    //根据当前登记审核
    @RequestMapping("/warehousingexaminequeryByGather.action")
    public Gather warehousingexaminequeryByGather(String gatherId){
      return gatherService.queryByGatherGatherdetailswarhousing(gatherId);
    }


    //登记审核成功  修改库存储存数量 和当前储存数量

    @RequestMapping("/updatewarehousingexaminequeryByGather.action")
    public Map updatewarehousingexaminequeryByGather(@RequestBody Gather gather){
        System.out.println(gather);
        //修改库存信息 数量和当前数量
        //创建map
        Map map=new HashMap();
        //修改改库存表为K002-4 通过
        boolean b=gatherService.updatewarehousingexaminequeryGatherByGathers(gather);
        //修改明细表
        boolean b1=gatherDetailsService.updatewarehousingexaminequeryGatherByGathers(gather.getGatherDetails(),gather.getId());
        //修改库存信息 数量 和当前储存数量
        boolean f=cellService.updatewarehousingexaminequeryCellByGathers(gather.getGatherDetails());
        //判断
        map.put("msg","登记审核失败");
        if(b && f){
            map.put("msg","登记审核成功");
        }
        return map;
    }


    //登记查询 查询所以登记的记录
    @RequestMapping("/queryGatherallwarehousing.action")
    public PageVo<Gather> queryGatherallwarehousing(@RequestParam(value = "pageno",defaultValue = "1") int pageno,
                                                    @RequestParam(value = "pagesize",defaultValue = "5") int pagesize){
        //创建pagevo
        PageVo<Gather> pageVo=new PageVo<Gather>();
        //获取方法
       IPage<Gather> iPage=gatherService.queryGatherallwarehousing(pageno,pagesize);
       //赋值
        pageVo.setTotal(iPage.getTotal());
        pageVo.setRows(iPage.getRecords());
        return pageVo;
    }

}


